
import { _decorator, Component, Node, Vec3, UITransform } from 'cc';
import {Data} from "./Data";
import {Tool} from "./Tool";
import {Game} from "./Game";
const { ccclass, property } = _decorator;

@ccclass('Pipes')
export class Pipes extends Component {
    /**
     * 存储up和down子节点的数组
     * @private
     */
    private up: Node[] = new Array<Node>(Data.Pipe.childNum / 2);
    private down: Node[] = new Array<Node>(Data.Pipe.childNum / 2);
    /**
     * 最前方 最后放图片索引
     */
    private frondIndex!: number;
    private backIndex!: number;
    /**
     * 对外初始化接口
     */
    public init(){
        this.setChildProperties();
        this.initIndex();
        for (let i = 0; i < Data.Pipe.childNum / 2; i++){
            this.setRandPipe(this.up[i], this.down[i]);
        }
    }
    /**
     * 获取子节点到数组
     */
    private getChild(){
        for (let i = 0; i < Data.Pipe.childNum / 2; i++){
            this.up[i] = this.node.getChildByName('PipeUp_0' + (i + 1).toString())!;
            this.down[i] = this.node.getChildByName('PipeDown_0' + (i + 1).toString())!;
        }
    }
    /**
     * 设置子节点初始属性
     */
    private setChildProperties(){
        for (let i = 0; i < Data.Pipe.childNum / 2; i++){
            this.up[i].setPosition(new Vec3(i * Data.Pipe.distance, -Data.Pipe.yPos + 112));
            this.down[i].setPosition(new Vec3(i * Data.Pipe.distance, Data.Pipe.yPos + 112));
        }
    }
    /**
     * 初始化 frontIndex backIndex
     */
    private initIndex(){
        this.frondIndex = 0;
        this.backIndex = Data.Pipe.childNum / 2 - 1;
    }
    /**
     * 更新frontIndex backIndex
     * @private
     */
    private updateIndex(){
        this.frondIndex++;
        this.backIndex++;
        if (this.frondIndex == Data.Pipe.childNum / 2){
            this.frondIndex = 0;
        }
        if (this.backIndex == Data.Pipe.childNum / 2){
            this.backIndex = 0;
        }
    }
    /**
     * 向左移动
     */
    private leftMove(dt: number){
        for (let i = 0; i < Data.Pipe.childNum / 2; i++){
            this.up[i].setPosition(new Vec3(this.up[i].position.x + Data.Pipe.xSpeed * dt, -Data.Pipe.yPos + 112));
            this.down[i].setPosition(new Vec3(this.down[i].position.x + Data.Pipe.xSpeed * dt, Data.Pipe.yPos + 112));
        }
    }
    /**
     * 判断是否需要重置最前方图片的位置
     */
    private  isNeedResetFrontPos() : boolean {
        return this.up[this.frondIndex].position.x < -2 * Data.Pipe.distance ||
            this.down[this.frondIndex].position.x < -2 * Data.Pipe.distance;
    }
    /**
     * 重置最前方图片的位置
     */
    private resetFrontPos(){
        this.up[this.frondIndex].setPosition(new Vec3(this.up[this.backIndex].position.x + Data.Pipe.distance, -Data.Pipe.yPos + 112));
        this.down[this.frondIndex].setPosition(new Vec3(this.down[this.backIndex].position.x + Data.Pipe.distance, Data.Pipe.yPos + 112));
        this.updateIndex();
    }

    /**
     * 随机设置 up 和 down的高度和间隙
     */
    private setRandPipe(up: Node, down: Node) {
        let height: number = Tool.Algorithm.getRandNum(Data.Pipe.minHeight, Data.Pipe.maxHeight);
        let padding: number = Tool.Algorithm.getRandNum(Data.Pipe.minPadding, Data.Pipe.maxPadding);
        up.getComponent(UITransform)!.height = height;
        down.getComponent(UITransform)!.height = Data.Bg.height - padding - height;
    }
    /**
     * 子类实现
     */
    protected onLoad() {
        this.getChild();
    }

    protected start() {
        this.setChildProperties();
        this.initIndex();
        for (let i = 0; i < Data.Pipe.childNum / 2; i++){
            this.setRandPipe(this.up[i], this.down[i]);
        }
    }

    protected update(dt: number) {
        if (!Game.isPaused){
            this.leftMove(dt);
            if (this.isNeedResetFrontPos()){
                this.setRandPipe(this.up[this.frondIndex], this.down[this.frondIndex]);
                this.resetFrontPos();
            }
        }

    }
}